package DCSource.Template.CMDEngineWorker;

import java.util.ArrayList;
import java.util.Arrays;

import DCSource.Template.CMD;
import DCSource.Template.ConsoleCMD;

public class MainListener implements DCSource.Template.Input.Transmitter{
	private ArrayList<Command> list = new ArrayList<Command>();
	private ArrayList<MainCommand> list2 = new ArrayList<MainCommand>();
	public DCSource.ConfigManager.Class Configs = new DCSource.ConfigManager.Class("Temp");
	public void add(Command it){
		list.add(it);
	}
	public void remove(Command it){
		list.remove(it);
	}
	public void addMain(MainCommand it){
		list2.add(it);
	}
	public void removeMain(MainCommand it){
		list2.remove(it);
	}
	public Command[] getall(){
		return list.toArray(new Command[list.size()]);
	}
	public Command get(String name){
		for (Command b:list){
			if (b.CalledName().equals(name)){
				return b;
			}
		}
		return null;
	}
	public MainCommand[] getallMain(){
		return list2.toArray(new MainCommand[list.size()]);
	}
	public MainCommand getMain(String name){
		for (MainCommand b:list2){
			if (b.CalledName().equals(name)){
				return b;
			}
		}
		return null;
	}
	public void get(String getting, CMD console) {
		String[] inputs = getting.split(" ");
		if (inputs.length>0){
			String name = inputs[0].replace("/", "").replace("\\", "");
			String[] args = new String[]{};
			String[] values = new String[]{};
			int count=0;
			int count2=0;
			ArrayList<String> argslist = new ArrayList<String>();
			ArrayList<String> valueslist = new ArrayList<String>();
			for (int i=1;i<inputs.length;i++){
				if (inputs[i].startsWith("/")){
					count++;
					argslist.add(inputs[i].substring(1));
				}else{
					count2++;
					valueslist.add(inputs[i]);
				}
			}
			args = new String[count];
			values = new String[count2];
			String[] cacha = Arrays.copyOf(argslist.toArray(), argslist.toArray().length, String[].class);
			String[] cachb = Arrays.copyOf(valueslist.toArray(), valueslist.toArray().length, String[].class);
			System.arraycopy(cacha, 0, args, 0, cacha.length);
			System.arraycopy(cachb, 0, values, 0, cachb.length);
			if (name!=""){
				for (MainCommand b : list2){
					String[] lines = b.OnCommand(name, args, values);
					for (int o=0;o<lines.length;o++){
						console.writeLn(lines[o]);
					}
				}
			}
			for (int I=0;I<list.size();I++){
				if (list.get(I).CalledName().equalsIgnoreCase(name)){
					String[] lines = list.get(I).OnCommand(args, values);
					for (int o=0;o<lines.length;o++){
						console.writeLn(lines[o]);
					}
				}
			}
		}
	}
	public void get(String getting, ConsoleCMD console){
		String[] inputs = getting.split(" ");
		if (inputs.length>0){
			String name = inputs[0].replace("/", "").replace("\\", "");
			String[] args = new String[]{};
			String[] values = new String[]{};
			int count=0;
			int count2=0;
			ArrayList<String> argslist = new ArrayList<String>();
			ArrayList<String> valueslist = new ArrayList<String>();
			for (int i=1;i<inputs.length;i++){
				if (inputs[i].startsWith("/")){
					count++;
					argslist.add(inputs[i].substring(1));
				}else{
					count2++;
					valueslist.add(inputs[i]);
				}
			}
			args = new String[count];
			values = new String[count2];
			String[] cacha = Arrays.copyOf(argslist.toArray(), argslist.toArray().length, String[].class);
			String[] cachb = Arrays.copyOf(valueslist.toArray(), valueslist.toArray().length, String[].class);
			System.arraycopy(cacha, 0, args, 0, cacha.length);
			System.arraycopy(cachb, 0, values, 0, cachb.length);
			if (name!=""){
				for (MainCommand b : list2){
					String[] lines = b.OnCommand(name, args, values);
					for (int o=0;o<lines.length;o++){
						console.writeln(lines[o]);
					}
				}
			}
			for (int I=0;I<list.size();I++){
				if (list.get(I).CalledName().equalsIgnoreCase(name)){
					String[] lines = list.get(I).OnCommand(args, values);
					for (int o=0;o<lines.length;o++){
						console.writeln(lines[o]);
					}
				}
			}
		}
	}
}
